package cutpart_queue

import (
	"database/sql"
	"fmt"
	"jihe.common/protos/calculate_server/proto"
)

func (s *Tx) BatchCreate(in *proto.CalculateCutpartQueues) (err error) {
	sqlStr := fmt.Sprintf("INSERT INTO %s (need_id, cutpart_id, obj_url, code, uv_width, befor_obj_url, dxf_url, straight_line_idx, gap_idx, dxf_info_url, interior_info)VALUES($1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11)", s.Name)
	stmt, err := s.Db.Prepare(sqlStr)
	if err != nil {
		return
	}
	defer stmt.Close()
	for _, v := range in.List {
		_, err = stmt.Exec(v.NeedId, v.CutpartId, v.ObjUrl, v.Code, v.UvWidth, v.BeforObjUrl, v.DxfUrl, v.StraightLineIdx, v.GapIdx, v.DxfInfoUrl, v.InteriorInfo)
		if err != nil {
			return
		}
	}
	return
}

func (s *Tx) ExistNeedId(needId string) bool {
	sqlStr := fmt.Sprintf("select count(0) from %s where need_id = $1", s.Name)
	count := 0
	s.Db.QueryRow(sqlStr, needId).Scan(&count)
	return count != 0
}

func (s *Model) GetByNeedId(needId string) (ret []*proto.CalculateCutpartQueue, err error) {
	sqlStr := fmt.Sprintf("select id, need_id, cutpart_id, status from %s where need_id = $1", s.Name)
	rows, err := s.Db.Query(sqlStr, needId)
	if err != nil {
		return
	}
	defer rows.Close()
	for rows.Next() {
		item := &proto.CalculateCutpartQueue{}
		err = rows.Scan(&item.Id, &item.NeedId, &item.CutpartId, &item.Status)
		if err != nil {
			return
		}
		ret = append(ret, item)
	}
	return
}

func (s *Model) GetNextWait() (ret *proto.CalculateCutpartQueue, err error) {
	ret = new(proto.CalculateCutpartQueue)
	sqlStr := fmt.Sprintf("select id, need_id, cutpart_id, status, obj_url, code, uv_width, befor_obj_url, dxf_url, straight_line_idx, gap_idx, dxf_url, dxf_info_url, interior_info from %s where status = 1 order by id limit 1", s.Name)
	err = s.Db.QueryRow(sqlStr).Scan(&ret.Id, &ret.NeedId, &ret.CutpartId, &ret.Status, &ret.ObjUrl, &ret.Code, &ret.UvWidth, &ret.BeforObjUrl, &ret.DxfUrl, &ret.StraightLineIdx, &ret.GapIdx, &ret.DxfUrl, &ret.DxfInfoUrl, &ret.InteriorInfo)
	if err != nil {
		if err == sql.ErrNoRows {
			err = nil
			return
		}
		return
	}
	return
}

func (s *Model) UpdateStatusById(id, status int64) (err error) {
	sqlStr := fmt.Sprintf("update %s set status = $1 where id = $2", s.Name)
	_, err = s.Db.Exec(sqlStr, status, id)
	if err != nil {
		return
	}
	return
}

func (s *Model) DeleteByNeedId(needId string) (err error) {
	sqlStr := fmt.Sprintf("delete from %s where need_id = $1", s.Name)
	_, err = s.Db.Exec(sqlStr, needId)
	if err != nil {
		return
	}
	return
}

func (s *Model) GetCountByMiniId(miniId int64) (count int64) {
	sqlStr := fmt.Sprintf("select count(0) from %s where id < $1 and (status = 1 or status = 2)", s.Name)
	err := s.Db.QueryRow(sqlStr, miniId).Scan(&count)
	if err != nil {
		return
	}
	return
}

func (s *Tx) UpdateStatusById(id, status int64) (err error) {
	sqlStr := fmt.Sprintf("update %s set status = $1 where id = $2", s.Name)
	_, err = s.Db.Exec(sqlStr, status, id)
	if err != nil {
		return
	}
	return
}

func (s *Model) UpdateStatusByNeedId(needId string) (err error) {
	//计算中的不能直接取消
	sqlStr := fmt.Sprintf("update %s set status = 3 where need_id = $1 and status = 1", s.Name)
	_, err = s.Db.Exec(sqlStr, needId)
	if err != nil {
		return
	}
	return
}

func (s *Tx) UpdateCancelNeedId(needId string) (err error) {
	sqlStr := fmt.Sprintf("update %s set status = 3 where need_id = $1 and status != 4 and status != 5", s.Name)
	_, err = s.Db.Exec(sqlStr, needId)
	if err != nil {
		return
	}
	return
}
